/*
 * Copyright (C) 2011, EADS France
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */
package xowl.gmi.view.diagram;

/**
 *
 * @author Laurent WOUTERS
 */
public class Image extends Widget {
    private javax.swing.ImageIcon icon;
    
    public void setIcon(javax.swing.ImageIcon icon) { this.icon = icon; }
    
    public Image(javax.swing.ImageIcon icon) {
        this.icon = icon;
        this.dimension = new Couple(icon.getIconWidth(), icon.getIconHeight());
    }
    
    @Override public Couple getMinSize() { return dimension; }
    @Override public Couple getMaxSize() { return dimension; }
    @Override public Couple layout() { return dimension; }
    @Override public Couple layout(Couple bounds) { return dimension; }
    
    @Override public void paint(java.awt.Graphics2D g2d, float alpha, DiagramStyle style, Couple offset) {
        Couple r = position.plus(offset);
        g2d.drawImage(icon.getImage(), (int)r.x, (int)r.y, icon.getImageObserver());
    }
    @Override protected void exportSVG(org.w3c.dom.Document doc, org.w3c.dom.Element parent, float alpha, DiagramStyle style, Couple offset) {
        Couple r = position.plus(offset);
        org.w3c.dom.Element elem = doc.createElement("image");
        elem.setAttribute("x", Double.toString(r.x));
        elem.setAttribute("y", Double.toString(r.y));
        elem.setAttribute("width", Double.toString(dimension.x));
        elem.setAttribute("height", Double.toString(dimension.y));
        elem.setAttribute("xlink:href", getImageName(doc));
        parent.appendChild(elem);
    }
    
    private String getImageName(org.w3c.dom.Document doc) {
        org.w3c.dom.Node root = doc.getChildNodes().item(0);
        org.w3c.dom.Node first = root.getChildNodes().item(0);
        org.w3c.dom.Node dic = first;
        if (!"Resources".equals(first.getNodeName())) {
            dic = doc.createElement("Resources");
            root.insertBefore(dic, first);
        }
        for (int i=0; i!=dic.getChildNodes().getLength(); i++) {
            org.w3c.dom.Element item = (org.w3c.dom.Element)dic.getChildNodes().item(i);
            String attr = item.getAttribute("hash");
            int hash = Integer.parseInt(attr);
            if (icon.hashCode() == hash)
                return item.getAttribute("name");
        }
        org.w3c.dom.Element item = doc.createElement("image");
        String name = "image" + Integer.toString(dic.getChildNodes().getLength()+1) + ".png";
        item.setAttribute("name", name);
        item.setAttribute("hash", Integer.toString(icon.hashCode()));
        dic.appendChild(item);
        return name;
    }
}
